

package top.continew.admin.question.manger.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import top.continew.admin.question.manger.service.model.ValueVo;
import top.continew.admin.question.mapper.DictionaryMapper;
import top.continew.admin.question.model.Dictionary;
import top.continew.admin.question.manger.service.DictionaryService;
import top.continew.admin.question.manger.service.model.Value;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DictionaryServiceImpl implements DictionaryService {

    private final DictionaryMapper dictionaryMapper;

    public DictionaryServiceImpl(DictionaryMapper dictionaryMapper) {
        this.dictionaryMapper = dictionaryMapper;
    }

    @Override
    public List<Value> getList(String type) {
        return new Dictionary().selectList(new QueryWrapper<Dictionary>().eq(StrUtil.isNotEmpty(type), "type", type)
            .eq("status", "1")
            .orderByDesc("sort")).stream().map(s -> {
                Value value = new Value();
                value.setId(s.getId());
                value.setName(s.getName());
                return value;
            }).collect(Collectors.toList());
    }

    @Override
    public Map<String, List<ValueVo>> questionBank() {
        Map<String, List<ValueVo>> params = new HashMap<>();
        params.putAll(getSubjectAndLevelAndTestType());
        params.putAll(getQuestionType());
        params.putAll(getPaperType());
        return params;
    }

    @Override
    public Map<String, List<ValueVo>> questionBankView() {
        Map<String, List<ValueVo>> params = new HashMap<>();
        params.putAll(getQuestionType());
        params.putAll(getQuestionStatus());
        return params;
    }

    @Override
    public Map<String, List<ValueVo>> paper() {
        Map<String, List<ValueVo>> params = new HashMap<>();
        params.putAll(getSubjectAndLevelAndTestType());
        params.putAll(getPaperType());
        params.putAll(getPaperStatus());
        params.putAll(getIsPublic());
        return params;
    }

    @Override
    public Map<String, List<ValueVo>> question() {
        Map<String, List<ValueVo>> params = new HashMap<>();
        params.putAll(getSubjectAndLevelAndTestType());
        params.putAll(getQuestionType());
        params.putAll(getQuestionStatus());
        return params;
    }

    private Map<String, List<ValueVo>> getSubjectAndLevelAndTestType() {
        List<ValueVo> list = dictionaryMapper.getSubjectAndLevelAndTestType();
        return list.stream().collect(Collectors.groupingBy(ValueVo::getType));
    }

    private Map<String, List<ValueVo>> getQuestionType() {
        Map<String, List<ValueVo>> params = new HashMap<>(1);
        List<ValueVo> list = new ArrayList<>();
        // 1,选择题,2,多选题,3,判断题,4,情景题,5,简答题,6,辨析题,7,填空,8,完形填空,9,阅读理解,10 背景题
        list.add(new ValueVo("1", "选择题", "questionType"));
        list.add(new ValueVo("2", "多选题", "questionType"));
        list.add(new ValueVo("3", "判断题", "questionType"));
        list.add(new ValueVo("4", "情景题", "questionType"));
        list.add(new ValueVo("5", "简答题", "questionType"));
        list.add(new ValueVo("6", "辨析题", "questionType"));
        list.add(new ValueVo("7", "填空", "questionType"));
        list.add(new ValueVo("8", "完形填空", "questionType"));
        list.add(new ValueVo("9", "阅读理解", "questionType"));
        list.add(new ValueVo("10", "背景题", "questionType"));
        params.put("questionType", list);
        return params;
    }

    private Map<String, List<ValueVo>> getPaperType() {
        Map<String, List<ValueVo>> params = new HashMap<>(1);
        List<ValueVo> list = new ArrayList<>();
        // 1真题,2模拟题
        list.add(new ValueVo("1", "真题", "paperType"));
        list.add(new ValueVo("2", "模拟题", "paperType"));
        params.put("paperType", list);
        return params;
    }

    private Map<String, List<ValueVo>> getQuestionStatus() {
        Map<String, List<ValueVo>> params = new HashMap<>(1);
        List<ValueVo> list = new ArrayList<>();
        // 0,创建,1上线,2下线,3校验中,4,审核中,5,已审核
        list.add(new ValueVo("0", "创建", "questionStatus"));
        list.add(new ValueVo("1", "上线", "questionStatus"));
        list.add(new ValueVo("2", "下线", "questionStatus"));
        list.add(new ValueVo("3", "校验中", "questionStatus"));
        list.add(new ValueVo("4", "审核中", "questionStatus"));
        list.add(new ValueVo("5", "已审核", "questionStatus"));
        params.put("questionStatus", list);
        return params;
    }

    private Map<String, List<ValueVo>> getPaperStatus() {
        Map<String, List<ValueVo>> params = new HashMap<>(1);
        List<ValueVo> list = new ArrayList<>();
        // 状态:0,创建,1上线,2下线,3校验中,4,审核中,5,已审核
        list.add(new ValueVo("0", "创建", "paperStatus"));
        list.add(new ValueVo("1", "上线", "paperStatus"));
        list.add(new ValueVo("2", "下线", "paperStatus"));
        list.add(new ValueVo("3", "校验中", "paperStatus"));
        list.add(new ValueVo("4", "审核中", "paperStatus"));
        list.add(new ValueVo("5", "已审核", "paperStatus"));
        params.put("paperStatus", list);
        return params;
    }

    private Map<String, List<ValueVo>> getIsPublic() {
        Map<String, List<ValueVo>> params = new HashMap<>(1);
        List<ValueVo> list = new ArrayList<>();
        // 0内部,1公开
        list.add(new ValueVo("0", "内部", "isPublic"));
        list.add(new ValueVo("1", "公开", "isPublic"));
        params.put("isPublic", list);
        return params;
    }

    @Override
    public List<ValueVo> teacher(String name) {
        return this.dictionaryMapper.getTeacher(name);
    }

    @Override
    public List<ValueVo> area(String name) {
        return this.dictionaryMapper.getArea(name);
    }

    @Override
    public List<ValueVo> point(String name) {
        return this.dictionaryMapper.getPoint(name);
    }
}
